import crypto from 'crypto'
import {errorMessage,successMessage} from './Message'
import {obtainShortMessageVerification} from '../api/common'
/* md5加密 */
export function md5(value) {
  const md5 = crypto.createHash('md5');
  md5.update(value);
  return md5.digest("hex")
}

export function startLoading({app,text = 'Loading',spinner = 'el-icon-loading',background = 'rgba(0, 0, 0, 0.7)'}) {
  const loading = app.$loading({
    lock: app,
    text: text,
    spinner: spinner,
    background: background
  });
  return loading
}


export function sendMessage(phone,app) {
  if (!phone) return errorMessage('请输入电话号码！');
  let loading =  startLoading({app:app,text:'获取验证短信中........'});
  return new Promise((resolve, reject) => {
    obtainShortMessageVerification({phone: phone}).then(response => {
      successMessage('验证码已发送！');
      loading.close();
      resolve(true)
    }).catch(error =>{
      loading.close();
      errorMessage('验证码发送失败！');
      reject(false)
    })
  })
}

